<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>layui</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <link rel="stylesheet" href="./layui/css/layui.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>

<body>
    <div style="margin-bottom: 5px;">
        <div id="table-main">
            <span class="layui-btn" id="getselect">获取选中行</span>
            <table class="layui-table" id="idTest" lay-filter="demo"></table>
        </div>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-mini" lay-event="detail">查看</a>
            <a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
        </script>
        <script src="./layui/layui.js" charset="utf-8"></script>
        <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
        <script>
        layui.use(['table', 'jquery', 'form', 'layer'], function() {
            var table = layui.table;
            var $ = layui.jquery;
            var form = layui.form;
            var layer = layui.layer;
            var tableIns = table.render({ //其它参数在此省略
                elem: '#idTest',
                id: 'idTest',
                url: './demo1.json', // 注意：如果你直接复制所有代码到本地，数据请求需要本地返回数据
                cols: [
                    [
                        { checkbox: true, fixed: true },
                        { field: 'id', width: 80, sort: true, fixed: true, title: 'ID' },
                        { field: 'username', width: 80, title: '用户名' },
                        { field: 'sex', width: 80, sort: true, title: '性别' },
                        { field: 'city', width: 80, title: '城市' },
                        { field: 'sign', width: 177, title: '签名' },
                        { field: 'experience', width: 80, sort: true, title: '积分' },
                        { field: 'classify', width: 80, title: '职业' },
                        { field: 'wealth', width: 135, sort: true, title: '财富' },
                        { field: 'score', width: 80, sort: true, title: '评分' },
                        { fixed: 'right', width: 160, align: 'center', toolbar: '#barDemo' }
                    ]
                ],
                where: {}, //如果无需传递额外参数，可不加该参数
                limits: [10, 15, 20, 40, 60, 80],
                limit: 10,
                page: true, //开启分页
                done: function(res, curr, count) {
                    var data = res.data;
                    var allck = true;
                    for (var item in data) {
                        if (data[item].score == 57) { //关键点如果data中score包含57那么就不能全选
                            allck = false;
                        }
                        break;
                    }
                    if (!allck) {
                        $(".layui-table-header").find("input[name = 'layTableCheckbox'][lay-filter='layTableAllChoose']").each(function() {
                            $(this).attr("disabled", 'disabled').next().removeClass("layui-form-checked");
                            form.render('checkbox');
                        });
                    }
                    var i = 0;
                    $(".layui-table-body.layui-table-main").find("input[name='layTableCheckbox']").each(function() {
                        if (res.data[i].score == 57) { //关键点如果当前行数据中score包含57那么就不可选
                            $(this).attr("disabled", 'disabled').removeAttr("checked");
                            form.render('checkbox');
                        }
                        i++;
                    });
                    i = 0;
                    $(".layui-table-fixed.layui-table-fixed-l").find(".layui-table-body").find("input[name='layTableCheckbox']").each(function() {
                        if (res.data[i].score == 57) { //关键点如果当前行数据中score包含57那么就不可选
                            $(this).attr("disabled", 'disabled').removeAttr("checked");
                            form.render('checkbox');
                        }
                        i++;
                    });
                }
            });
            //监听表格复选框选择
            table.on('checkbox(demo)', function(obj) {
                console.log(obj)
            });
            $("#getselect").click(function() {
                var checkStatus = table.checkStatus('idTest'); //test即为基础参数id对应的值
                layer.alert(JSON.stringify(checkStatus.data));
            });
            //监听工具条
            table.on('tool(demo)', function(obj) {
                var data = obj.data;
                if (obj.event === 'detail') {
                    layer.msg('ID：' + data.id + ' 的查看操作');
                } else if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function(index) {
                        obj.del();
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    layer.alert('编辑行：<br>' + JSON.stringify(data))
                }
            });
        });
        </script>
    </div>
</body>

</html>